package com.aynu.stroke.handler;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.aynu.commons.domin.bo.RoutePlanResultBO;
import com.aynu.commons.domin.bo.TextValue;
import com.aynu.commons.utils.HttpClientUtils;
import com.aynu.stroke.handler.valuation.BasicValuation;
import com.aynu.stroke.handler.valuation.FuelCostValuation;
import com.aynu.stroke.handler.valuation.StartPriceValuation;
import com.aynu.stroke.handler.valuation.Valuation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class BaiduMapClient {
    @Value("${baidu.map.api}")
    private String api;
    @Value("${baidu.map.ak}")
    private String ak;

    private final static Logger logger = LoggerFactory.getLogger(BaiduMapClient.class);

    //调百度路径计算两点间的距离，和预估抵达时长
    public RoutePlanResultBO pathPlanning(String origins, String destinations) {
        //构造http请求
        Map<String, String> params = new HashMap<>();
        params.put("origins", origins);
        params.put("destinations", destinations);
        params.put("ak", ak);
        String s=null;
        try {
            s = HttpClientUtils.doGet(api, params);
        }catch (Exception e){
            e.printStackTrace();
        }
//        System.out.println(s);
//        {"status":0,"message":"成功","num":1,"mode":"driving","result":[{"distance":{"text":"1.3公里","value":1280},"duration":{"text":"1分钟","value":47}}]}
        //解析json字符串使用Jackson
        TextValue distance=new TextValue();
        TextValue duration=new TextValue();
        ObjectMapper mapper = new ObjectMapper();
        try {
            JsonNode rootNode = mapper.readTree(s);
            JsonNode resultArray  = rootNode.get("result");

            for (JsonNode node : resultArray) {
                // 获取 "distance" 对象
                JsonNode distanceNode = node.get("distance");
                // 获取 "duration" 对象
                JsonNode durationNode = node.get("duration");

                // 设置 "distance" 和 "duration" 的值
                distance.setText(distanceNode.get("text").asText());
                distance.setValue(distanceNode.get("value").asInt());

                duration.setText(durationNode.get("text").asText());
                duration.setValue(durationNode.get("value").asInt());
            }
        }catch (IOException e){
            e.printStackTrace();
        }

        RoutePlanResultBO resultBO=new RoutePlanResultBO();
        resultBO.setDistance(distance);
        resultBO.setDuration(duration);
        return resultBO;
    }
//测试方法
    public static void main(String[] args) {
        Valuation basicValuation = new BasicValuation(null);
        Valuation withFuelCost = new FuelCostValuation(basicValuation);
        Valuation withStartPrice = new StartPriceValuation(withFuelCost);

        float distance = 5; // 假设行驶了5公里
        float totalCost = withStartPrice.calculation(distance);
        System.out.println("Total cost for " + distance + " km: " + totalCost);

        Integer dis=1234567;
        float dis1= (float) dis /1000;
        float fee=withStartPrice.calculation(dis1);
        System.out.println(fee);
        DecimalFormat df=new DecimalFormat("#.00");

        String real=df.format(fee);
        System.out.println(real);
        System.out.println(new Date());

    }
}
